#! /usr/bin/octave -qf

# tells to octave that this file it's not a function file
1;

################################################################################
################################################################################
## action                                                                      #
################################################################################

function [nd_points, g_distance, spacing_value, spread_value, time_elapsed] = action(pareto_front_file, problem, mode)

    precision = 1000;

    fprintf("generating pareto front...\n");
    problem

    if (problem == "ZDT1" || problem == "ZDT2" || problem == "ZDT3")
        printf("i'm here\n");
        real_pareto_front = generate_pareto_front(precision, 30, problem);

    elseif (problem == "ZDT4" || problem == "ZDT6")
        real_pareto_front = generate_pareto_front(precision, 10, problem);

    elseif (problem == "ZDT5")
        real_pareto_front = generate_pareto_front(31, 80, problem);
    endif

    printf("[%s]\n", pareto_front_file);
    fprintf(stderr, "[%s]\n", pareto_front_file);
    load (pareto_front_file);

    __gnuplot_set__ term gif;

    set_plot_output_file = sprintf("__gnuplot_set__ output '%s_plot.gif';", pareto_front_file);

    eval(set_plot_output_file);

    x_real = real_pareto_front(:, 1);
    y_real = real_pareto_front(:, 2);

    x_nsga = pareto_front(:, 1);
    y_nsga = pareto_front(:, 2);

    if (problem == "ZDT5")
        plot(x_real, y_real, "*2;real pareto front;", x_nsga, y_nsga, "*6;front;");
    else
        plot(x_real, y_real, ".2;real pareto front;", x_nsga, y_nsga, "*6;front;");
    endif

    time_elapsed = elapsed_time;

    if (mode == "stat")

        nd_points = rows(pareto_front);
        printf("\tnondominated points %d\n", rows(pareto_front));
        fprintf(stderr, "\tnondominated points %d\n", rows(pareto_front));

        g_distance = generation_distance(real_pareto_front, pareto_front);
        printf("\tgeneration distance %f\n", g_distance);
        fprintf(stderr, "\tgeneration distance %f\n", g_distance);

        spacing_value = spacing(real_pareto_front, pareto_front);
        printf("\tspacing value %f\n", spacing_value);
        fprintf(stderr, "\tspacing value %f\n", spacing_value);

        extremes = zeros(2,2);
        extremes(1,:) = real_pareto_front(1, :);
        extremes(2,:) = real_pareto_front(rows(real_pareto_front)-1, :);

        spread_value = spread(extremes, pareto_front);
        printf("\tspread value %f\n", spread_value);
        fprintf(stderr, "\tspread value %f\n", spread_value);

        printf("\ttime elapsed %d\n", time_elapsed);
        fprintf(stderr, "\ttime elapsed %d\n", time_elapsed);
    else
        nd_points = 0;
        g_distance = 0;
        spacing_value = 0;
        spread_value = 0;
    endif

endfunction

## includes
source("zdt.m");
source("generation_distance.m");
source("spacing.m");
source("spread.m");

## main
if( nargin < 3 )
    fprintf(stderr,  "Usage: %s <stat|plot> <problem> <pareto_front_file(s)>\n", program_name );
    return;
endif
mode = argv{1}
problem = argv{2};
files_count = nargin -2;

non_dominated_points = zeros(files_count, 1);
generation_distances = zeros(files_count, 1);
spread_values = zeros(files_count, 1);
spacing_values = zeros(files_count, 1);
time_values = zeros(files_count, 1);


for i = 1 : files_count
    [nd_points, g_distance, spacing_value, spread_value, time_elapsed] = action(argv{i+2}, problem, mode);

    non_dominated_points(i) = nd_points;
    generation_distances(i) = g_distance;
    spacing_values(i) = spacing_value;
    spread_values(i) = spread_value;
    time_values(i) = time_elapsed;
endfor

if (mode == "stat")
    printf("\n\n[summary]\n\n");
    printf("\tnon dominated points average %f\n", mean(non_dominated_points));
    printf("\tnon dominated points standard deviation %f\n\n", std(non_dominated_points));

    printf("\tgeneration distance average %f\n", mean(generation_distances));
    printf("\tgeneration distance standard deviation %f\n\n", std(generation_distances));

    printf("\tspacing average %f\n", mean(spacing_values));
    printf("\tspacing standard deviation %f\n\n", std(spacing_values));

    printf("\tspread average %f\n", mean(spread_values));
    printf("\tspread standard deviation %f\n\n", std(spread_values));

    printf("\ttime average %f\n", mean(time_values));
    printf("\ttime standard deviation %f\n\n", std(time_values));

endif
